const {ccclass, property} = cc._decorator;

@ccclass
export default class Move_camera_Game extends cc.Component {

    @property({displayName: "相机", tooltip: "相机", type: cc.Node})
    camera: cc.Node = null;

    @property({displayName: "背景", tooltip: "背景", type: cc.Node})
    bg: cc.Node = null;

    @property({displayName: "角色", tooltip: "角色", type: cc.Node})
    player: cc.Node = null;

    update () {
        // 左边四分之三屏幕位置
        let left_3_4 = this.camera.x - (this.camera.width / 4);
        // 右边四分之三屏幕位置
        let right_3_4 = this.camera.x + (this.camera.width / 4);

        // 如果到了右边四分之三屏幕位置
        if (this.player.x >= right_3_4) {
            // cc.log("到了右边四分之三处");
            // 相机跟随角色移动
            this.camera.x = this.player.x - (this.camera.width / 4);
        }
        // 如果到了左边四分之三屏幕位置
        if (this.player.x <= left_3_4) {
            // cc.log("到了左边四分之三处");
            // 相机跟随角色移动
            this.camera.x = this.player.x + (this.camera.width / 4);
        }



        // 背景左
        let left_bg = this.bg.x - (this.bg.width / 2);
        // 背景右
        let right_bg = this.bg.x + (this.bg.width / 2);

        // 相机左
        let left_cam = this.camera.x - (this.camera.width / 2);
        // 相机右
        let right_cam = this.camera.x + (this.camera.width / 2);

        // 如果相机到达左边缘
        if (left_cam <= left_bg) {
            // 一直在边缘
            this.camera.x = left_bg + (this.camera.width / 2);
            // cc.log("左边到达边缘");
        }

        // 如果相机到达右边缘
        if (right_cam >= right_bg) {
            // 一直在边缘
            this.camera.x = right_bg - (this.camera.width / 2);
            // cc.log("右边到达边缘");
        }
    }

}
